package com.mdxq.mall.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.mdxq.mall.order.dto.UpdateDeliveryDTO;
import com.mdxq.mall.order.dto.UpdateNoteDTO;
import com.mdxq.mall.order.dto.UpdateReceiverInfoDTO;
import com.mdxq.mall.order.entity.OrderOperateHistory;
import com.mdxq.mall.order.mapper.OrderOperateHistoryMapper;
import com.mdxq.mall.order.service.OrderOperateHistoryService;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mdxq.mall.order.entity.Order;
import com.mdxq.mall.order.mapper.OrderMapper;
import com.mdxq.mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 订单表 服务层实现。
 *
 * @author zdy
 * @since 2025-01-19
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderOperateHistoryMapper historyMapper;
    @Transactional
    @Override
    public boolean updateReceiverInfo(Order order, String token) {
        //先更新订单表
        mapper.update(order,true);//忽略订单中的空属性
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(order.getId());
        history.setOrderStatus(order.getStatus());
        history.setCreateTime(LocalDateTime.now());
        history.setNote("修改了订单的收件人信息");
        history.setOperateMan(JWTUtil.parseToken(token).getPayload("username").toString());
        historyMapper.insert(history);
        //在新增管理员操作记录oms_order_operate_history
        return true;
    }

    @Override
    @Transactional
    public boolean updateNote(UpdateNoteDTO dto, String token) {
        Order order = UpdateEntity.of(Order.class,dto.getId());
        order.setNote(dto.getNote());
        mapper.update(order,true);
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderId(order.getId());
        history.setOrderStatus(dto.getStatus());
        history.setCreateTime(LocalDateTime.now());
        history.setNote(String.format("修改了订单的备注内容：%s",dto.getNote()));
        history.setOperateMan(JWTUtil.parseToken(token).getPayload("username").toString());
        historyMapper.insert(history);
        return true;
    }

    @Override
    @Transactional
    public boolean updateDelivery(List<UpdateDeliveryDTO> dtoList, String token) {
        OrderOperateHistory history = new OrderOperateHistory();
        history.setOrderStatus(2);
        history.setCreateTime(LocalDateTime.now());
        history.setNote("订单发货");
        history.setOperateMan(JWTUtil.parseToken(token).getPayload("username").toString());
        historyMapper.insert(history);
        for (UpdateDeliveryDTO dto : dtoList) {
            Order order = UpdateEntity.of(Order.class,dto.getOrderId());
            order.setStatus(2);
            order.setDeliveryCompany(dto.getDeliveryCompany());
            order.setDeliverySn(dto.getDeliverySn());
            mapper.update(order,true);
            history.setOrderId(order.getId());
            historyMapper.insert(history);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean removeByIds(List<String> idList, String token) {
        removeByIds(idList);
        for (String orderId : idList) {
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(Long.valueOf(orderId));
            history.setOrderStatus(5);
            history.setCreateTime(LocalDateTime.now());
            history.setNote("删除订单");
            history.setOperateMan(JWTUtil.parseToken(token).getPayload("username").toString());
            historyMapper.insert(history);
        }
        return true;
    }

    @Override
    public boolean updateClose(String ids, String note, String token) {
        List<String> idList = Arrays.asList(ids.split(","));
        for (String orderId : idList) {
            Order order = UpdateEntity.of(Order.class,Long.valueOf(orderId));
            order.setStatus(4);
            order.setNote(note);
            mapper.update(order,true);
            OrderOperateHistory history = new OrderOperateHistory();
            history.setOrderId(Long.valueOf(orderId));
            history.setOrderStatus(5);
            history.setCreateTime(LocalDateTime.now());
            history.setNote("关闭订单");
            history.setOperateMan(JWTUtil.parseToken(token).getPayload("username").toString());
            historyMapper.insert(history);
        }
        return true;
    }

}
